<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\CommController;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Crypt;
use App\Http\Model\User;
require_once 'resources/views/org/code/Code.class.php';

class LoginController extends CommController
{
    //
    public function login(){
        if($input=Input::all()){
            $code=new \Code();
            $_code=$code->get();
            if(strtoupper($input['code'])!=$_code){
                return back()->with('msg','验证码错误');
            }else {
                $isAuth=false;
                $user=User::where('username',$input['username'])->get();
                foreach ($user as $one){
                    if(Crypt::decrypt($one['password'])==$input['password']){
                        session(['user'=>$one]);
                        $isAuth=true;
                        break;
                    }
                }
                if($isAuth){
//                     return view('admin.index');
                    return redirect('admin');
                }else {
                    return back()->with('msg','用户名或密码错误');
                }
            }
        }else {
            return view('admin.login');
        }
    }
    public function code(){
        $code=new \Code();
        $code->make();
    }
//     public function test(){
//         echo Crypt::encrypt('123');
//     }
}
